﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        DataTable dt = null;
        public Form1()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO:  这行代码将数据加载到表“bDataSet.Articles”中。您可以根据需要移动或删除它。
            //this.articlesTableAdapter.Fill(this.bDataSet.Articles);
            if (this.comboBox1.Items.Count > 0)
                this.comboBox1.SelectedIndex = 0;
            if (this.comboBox2.Items.Count > 0)
                this.comboBox2.SelectedIndex = 0;
            this.label1.Text = Program.name;
            Data_Load();

        }

        private void Data_Load()
        {
            dt = Class1.getDataTable();//获取数据表
            this.dataGridView1.DataSource = dt;
        }


        //添加
        private void button1_Click(object sender, EventArgs e)
        {
            String tit = this.textBox1.Text;
            String CreatedTime = DateTime.Now.ToLocalTime().ToString();
            String Author = Program.name;
            int id = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                String strid = dataGridView1.Rows[i].Cells[0].Value.ToString();
                int temp = Convert.ToInt32(strid);
                if (temp > id)
                    id = temp;
            }
            id += 1;

            if (tit == null)
            {
                DialogResult dr = MessageBox.Show("标题不能为空！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            }
            else
            {

                Data.ExecuteNoQuery("insert into Articles(Id,Title,CreatedTime,Author)values(" + id.ToString() + ",'" + tit + "','" + CreatedTime + "','" + Author + "')");

                Data_Load();
            }

        }



        //删除
        private void button2_Click(object sender, EventArgs e)
        {
            int num = this.dataGridView1.SelectedRows.Count;
            if(num==0)
            {
                DialogResult dr = MessageBox.Show("未选中任何行，请点击行最左侧空白进行选中！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            }
            else
            {
        
                for (int i=0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Selected == true)
                    {
                        string strid = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        Data.ExecuteNoQuery("delete from Articles where Id="+strid);
                    }
                }
                Data_Load();
            }
        }
        //修改
        private void button3_Click(object sender, EventArgs e)
        {
            String tit = this.textBox2.Text;
            if (tit == "")
            {
                DialogResult dr = MessageBox.Show("请输入修改后的值！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                return;
            }
            int num = this.dataGridView1.SelectedRows.Count;
            if (num == 0)
            {
                DialogResult dr = MessageBox.Show("未选中任何行，请点击行最左侧空白进行选中！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                return;
            }
            else if (num > 1)
            {
                DialogResult dr = MessageBox.Show("不可同时编辑多行！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Selected == true)
                    {
                        string strid = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        String tiaojian = this.comboBox2.SelectedItem.ToString();
                        Data.ExecuteNoQuery("update Articles set " + tiaojian + "='" + textBox2.Text + "' where Id='" + strid + "'");
                        Data_Load();
                    }
                }

                
            }
        }
        //查询
        private void button4_Click(object sender, EventArgs e)
        {
            String tit = this.textBox4.Text;
            if (tit == "")
            {
                DialogResult dr = MessageBox.Show("请输入查询条件！！", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            }
            else
            {
                String tiaojian = this.comboBox1.SelectedItem.ToString();
                DataSet ds = Data.ExecuteDataSet("select * from Articles where " + tiaojian + "='" + tit+"'");
                dt = ds.Tables[0];
                this.dataGridView1.DataSource = dt;
                if (this.dataGridView1.Rows.Count==0)
                {
                    DialogResult dr = MessageBox.Show("没有对应条件数据", " 提示",
                        MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
                    Data_Load();
                }
              
                
                
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
